<?php
	// Usage:
	// php makewebsite.php <source directory> [<destination directory>]
	// Source directory: should contain the folders with movies
	// Destination directory: the directory in which the "website" should be created. If not given, current working directory.
	//                        this directory will be created if required.
	
	$folder = opendir($argv[1]);
	umask(0);
	if(isset($argv[2])){
		$webbasedir = $argv[2];
	}else{
		$webbasedir = getcwd()."/";
	}
	
	if(!file_exists($webbasedir)){
		mkdir($webbasedir,0777,1);
	}
	if(!file_exists($webbasedir."/index.htm")){
		makemainindex();
	}
	
	if(!file_exists($webbasedir."movies/")){
		mkdir($webbasedir."movies");
	}
	if(!file_exists($webbasedir."movies/covers/")){
		mkdir($webbasedir."movies/covers");
	}
	
	$temp = explode("/",$argv[1]);
	$basedirname = $temp[count($temp)-2];
	
	$allmovies = array();
	
	$allgenres	= array();
	$allyears	= array();
	$allrates	= array();
	
	while(false !== ($file = readdir($folder))){
		if(substr($file, 0, 1) == "."){ continue; }
		$tmp = scandir($argv[1]."/".$file);
		foreach($tmp as $df){
			if(substr($df,0,1) == "."){ continue; }
			if(strpos($df, "xml") > 1){
				$dom = new DomDocument();
				$dom->load($argv[1]."/".$file."/".$df);
				$temp = xml2array($dom);
				$dbg = fopen("/tmp/webdebug.log","a");
				fwrite($dbg, print_r($temp,1));
				//				$imdbnr = trim(implode("",file($argv[1]."/".$file."/imdbnr.txt")));
				$imdbnr = md5($file);
				if(file_exists($argv[1]."/".$file."/cover.jpg")){
					makecover($argv[1]."/".$file."/cover.jpg",$imdbnr);
				}
				makemoviepage($imdbnr,$temp);
				
				
				continue(2);
			}
		}
	}
	makeindexpage();
	makegenrepages();
	makeratepages();
	makeyearpages();
	
	function makeindexpage(){
		global $webbasedir, $allmovies;
		$htm = fopen($webbasedir."/mov_alphabetical.htm","w");
		
		$titles = array_keys($allmovies);
		sort($titles);
		fwrite($htm, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
			   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
			   <html xmlns="http://www.w3.org/1999/xhtml">
			   <head>
			   <title>My Movies</title>
			   <style type="text/css">
			   body	{ background: #555555; font-family: sans-serif; padding: 0px; margin: 0px; }
			   ul.btns	{ position: absolute; top: 60px; list-style: none; padding-left: 0px; width: 100%; }
			   ul.btns li	{ padding: 0px; margin: 0px; }
			   ul.btns li a { display: block; font-size: 48px; padding: 10px; color: #ffffff; text-decoration: none; }
			   a.backbtn	{ float:left; text-decoration: none; color: #aaaaaa; display: block; width: 100px; }
			   </style>
			   </head>
			   <body>
			   <div style="height: 60px; color: #aaaaaa; font-weight: bold; text-align:center; ; font-size:48px; background:#000000">MY MOVIES</div>
			   <ul class="btns">
			   ');
		foreach($titles as $title){
			fwrite($htm, '		<li><a href="movies/'.$allmovies[$title].'.htm">'.$title.'</a></li>'."\n");
		}
		fwrite($htm,'
			   </ul>
			   </body>
			   </html>');
	}
	
	function makegenrepages(){
		global $webbasedir, $allmovies, $allgenres;
		$htm = fopen($webbasedir."/mov_genres.htm","w");
		
		$genres = array_keys($allgenres);
		sort($genres);
		fwrite($htm, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
			   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
			   <html xmlns="http://www.w3.org/1999/xhtml">
			   <head>
			   <title>My Movies</title>
			   <style type="text/css">
			   body	{ background: #555555; font-family: sans-serif; padding: 0px; margin: 0px; }
			   ul.btns	{ position: absolute; top: 60px; list-style: none; padding-left: 0px; width: 100%; }
			   ul.btns li	{ padding: 0px; margin: 0px; }
			   ul.btns li a { display: block; font-size: 48px; padding: 10px; color: #ffffff; text-decoration: none; }
			   a.backbtn	{ float:left; text-decoration: none; color: #aaaaaa; display: block; width: 100px; }
			   </style>
			   </head>
			   <body>
			   <div style="height: 60px; color: #aaaaaa; font-weight: bold; text-align:center; font-size:48px; background:#000000">MY MOVIES</div>
			   <ul class="btns">
			   ');
		foreach($genres as $genre){
			if(substr($genre,0,4) == "IMDb"){ continue; }
			fwrite($htm, '		<li><a href="mov_genre_'.str_replace(" ","_",$genre).'.htm">'.$genre.'<div style="float:right">('.count($allgenres[$genre]).')</div></a></li>'."\n");
			makegenrepage($genre);
		}
		fwrite($htm,'
			   </ul>
			   </body>
			   </html>');
	}
	
	function makegenrepage($genre){
		global $webbasedir, $allmovies, $allgenres;
		$htm = fopen($webbasedir.'/mov_genre_'.str_replace(" ","_",$genre).".htm","w");
		
		$titles = $allgenres[$genre];
		sort($titles);
		
		fwrite($htm, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
			   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
			   <html xmlns="http://www.w3.org/1999/xhtml">
			   <head>
			   <title>My Movies</title>
			   <style type="text/css">
			   body	{ background: #555555; font-family: sans-serif; padding: 0px; margin: 0px; }
			   ul.btns	{ position: absolute; top: 60px; list-style: none; padding-left: 0px; width: 100%; }
			   ul.btns li	{ padding: 0px; margin: 0px; }
			   ul.btns li a { display: block; font-size: 48px; padding: 10px; color: #ffffff; text-decoration: none; }
			   a.backbtn	{ float:left; text-decoration: none; color: #aaaaaa; display: block; width: 100px; }
			   </style>
			   </head>
			   <body>
			   <div style="height: 60px; color: #aaaaaa; font-weight: bold; text-align:center; font-size:48px; background:#000000">'.$genre.'</div>
			   <ul class="btns">
			   ');
		foreach($titles as $title){
			fwrite($htm, '		<li><a href="movies/'.$allmovies[$title].'.htm">'.$title.'</a></li>'."\n");
		}
		fwrite($htm,'
			   </ul>
			   </body>
			   </html>');
	}
	
	
	
	function makeyearpages(){
		global $webbasedir, $allmovies, $allyears;
		$htm = fopen($webbasedir."/mov_years.htm","w");
		
		$years = array_keys($allyears);
		sort($years);
		fwrite($htm, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
			   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
			   <html xmlns="http://www.w3.org/1999/xhtml">
			   <head>
			   <title>My Movies</title>
			   <style type="text/css">
			   body	{ background: #555555; font-family: sans-serif; padding: 0px; margin: 0px; }
			   ul.btns	{ position: absolute; top: 60px; list-style: none; padding-left: 0px; width: 100%; }
			   ul.btns li	{ padding: 0px; margin: 0px; }
			   ul.btns li a { display: block; font-size: 48px; padding: 10px; color: #ffffff; text-decoration: none; }
			   a.backbtn	{ float:left; text-decoration: none; color: #aaaaaa; display: block; width: 100px; }
			   </style>
			   </head>
			   <body>
			   <div style="height: 60px; color: #aaaaaa; font-weight: bold; text-align:center; font-size:48px; background:#000000">MY MOVIES</div>
			   <ul class="btns">
			   ');
		foreach($years as $year){
			fwrite($htm, '		<li><a href="mov_year_'.$year.'.htm">'.$year.'<div style="float:right">('.count($allyears[$year]).')</div></a></li>'."\n");
			makeyearpage($year);
		}
		fwrite($htm,'
			   </ul>
			   </body>
			   </html>');
	}
	
	function makeyearpage($year){
		global $webbasedir, $allmovies, $allyears;
		$htm = fopen($webbasedir.'/mov_year_'.$year.".htm","w");
		
		$titles = $allyears[$year];
		sort($titles);
		
		fwrite($htm, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
			   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
			   <html xmlns="http://www.w3.org/1999/xhtml">
			   <head>
			   <title>My Movies</title>
			   <style type="text/css">
			   body	{ background: #555555; font-family: sans-serif; padding: 0px; margin: 0px; }
			   ul.btns	{ position: absolute; top: 60px; list-style: none; padding-left: 0px; width: 100%; }
			   ul.btns li	{ padding: 0px; margin: 0px; }
			   ul.btns li a { display: block; font-size: 48px; padding: 10px; color: #ffffff; text-decoration: none; }
			   a.backbtn	{ float:left; text-decoration: none; color: #aaaaaa; display: block; width: 100px; }
			   </style>
			   </head>
			   <body>
			   <div style="height: 60px; color: #aaaaaa; font-weight: bold; text-align:center; font-size:48px; background:#000000">Year: '.$year.'</div>
			   <ul class="btns">
			   ');
		foreach($titles as $title){
			fwrite($htm, '		<li><a href="movies/'.$allmovies[$title].'.htm">'.$title.'</a></li>'."\n");
		}
		fwrite($htm,'
			   </ul>
			   </body>
			   </html>');
	}
	
	
	
	
	function makeratepages(){
		global $webbasedir, $allmovies, $allrates;
		$htm = fopen($webbasedir."/mov_rates.htm","w");
		
		$rates = array_keys($allrates);
		sort($rates);
		fwrite($htm, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
			   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
			   <html xmlns="http://www.w3.org/1999/xhtml">
			   <head>
			   <title>My Movies</title>
			   <style type="text/css">
			   body	{ background: #555555; font-family: sans-serif; padding: 0px; margin: 0px; }
			   ul.btns	{ position: absolute; top: 60px; list-style: none; padding-left: 0px; width: 100%; }
			   ul.btns li	{ padding: 0px; margin: 0px; }
			   ul.btns li a { display: block; font-size: 48px; padding: 10px; color: #ffffff; text-decoration: none; }
			   a.backbtn	{ float:left; text-decoration: none; color: #aaaaaa; display: block; width: 100px; }
			   </style>
			   </head>
			   <body>
			   <div style="height: 60px; color: #aaaaaa; font-weight: bold; text-align:center; font-size:48px; background:#000000">MY MOVIES</div>
			   <ul class="btns">
			   ');
		foreach($rates as $rate){
			fwrite($htm, '		<li><a href="mov_rates_'.$rate.'.htm">IMDB rating '.$rate.'-'.($rate+1).'<div style="float:right">('.count($allrates[$rate]).')</div></a></li>'."\n");
			makeratepage($rate);
		}
		fwrite($htm,'
			   </ul>
			   </body>
			   </html>');
	}
	
	function makeratepage($rate){
		global $webbasedir, $allmovies, $allrates;
		$htm = fopen($webbasedir.'/mov_rates_'.$rate.".htm","w");
		
		$titles = $allrates[$rate];
		sort($titles);
		
		fwrite($htm, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
			   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
			   <html xmlns="http://www.w3.org/1999/xhtml">
			   <head>
			   <title>My Movies</title>
			   <style type="text/css">
			   body	{ background: #555555; font-family: sans-serif; padding: 0px; margin: 0px; }
			   ul.btns	{ position: absolute; top: 60px; list-style: none; padding-left: 0px; width: 100%; }
			   ul.btns li	{ padding: 0px; margin: 0px; }
			   ul.btns li a { display: block; font-size: 48px; padding: 10px; color: #ffffff; text-decoration: none; }
			   a.backbtn	{ float:left; text-decoration: none; color: #aaaaaa; display: block; width: 100px; }
			   </style>
			   </head>
			   <body>
			   <div style="height: 60px; color: #aaaaaa; font-weight: bold; text-align:center; font-size:48px; background:#000000">IMDB RATING '.$rate.' - '.($rate+1).'</div>
			   <ul class="btns">
			   ');
		foreach($titles as $title){
			fwrite($htm, '		<li><a href="movies/'.$allmovies[$title].'.htm">'.$title.'</a></li>'."\n");
		}
		fwrite($htm,'
			   </ul>
			   </body>
			   </html>');
	}
	
	
	
	function makecover($source, $imdbnr){
		global $webbasedir;
		
		copy($source, $webbasedir."movies/covers/".$imdbnr.".jpg");
		exec("sips -Z 400 '".$webbasedir."movies/covers/".$imdbnr.".jpg'");
		/*
		 -- code below doesn't seem to work on OS X (no GD lib in the build-in PHP)
		 $im = imagecreatefromjpeg($source);
		 $mw = 350;
		 $mh = 150;
		 $w = imagesx($im);
		 $h = imagesy($im);
		 $scale = min($mw/$w, $mh/$h);
		 $dest = imagecreatetruecolor($w*$scale,$h*$scale);
		 imagecopyresized($dest, $im, 0, 0, 0, 0, $w*$scale, $h*$scale, $w, $h);
		 imagejpeg($dest,$webbasedir."movies/covers/".$imdbnr.".jpg");
		 */
	}
	
	function makemoviepage($imdbnr,$moviearray){
		global $webbasedir, $allmovies, $allgenres, $allrates, $allyears;
		$moviearray["media"][0]["title"][0]["cdata"] = preg_replace("/\/.*/","",$moviearray["media"][0]["title"][0]["cdata"]);
		
		$allmovies[$moviearray["media"][0]["title"][0]["cdata"]] = $imdbnr;
		
		if(!isset($allyears[substr($moviearray["media"][0]["published"][0]["cdata"],0,4)])){ $allyears[substr($moviearray["media"][0]["published"][0]["cdata"],0,4)] = array(); }
		array_push($allyears[substr($moviearray["media"][0]["published"][0]["cdata"],0,4)],$moviearray["media"][0]["title"][0]["cdata"]);
		
		if(!isset($allrates[floor($moviearray["media"][0]["imdbRating"][0]["cdata"])])){ $allrates[floor($moviearray["media"][0]["imdbRating"][0]["cdata"])] = array(); }
		array_push($allrates[floor($moviearray["media"][0]["imdbRating"][0]["cdata"])],$moviearray["media"][0]["title"][0]["cdata"]);
		
		
		$htm = fopen($webbasedir."movies/".$imdbnr.".htm","w");
		fwrite($htm,'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
			   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
			   <html xmlns="http://www.w3.org/1999/xhtml">
			   <head>
			   <title>My Movies</title>
			   <style type="text/css">
			   body	{ background: #555555; font-family: sans-serif; padding: 0px; margin: 0px; }
			   a.backbtn	{ float:left; text-decoration: none; color: #aaaaaa; display: block; width: 100px; }
			   td	{ color: #ffffff; }
			   </style>
			   </head>
			   <body>
			   <div style="color: #aaaaaa; font-weight: bold; text-align:center; font-size:48px; background:#000000">'.$moviearray["media"][0]["title"][0]["cdata"].'</div>
			   <div style="top: 60px; left: 0px; padding: 10px;">
			   <table style="width: 100%">
			   ');
		if(file_exists($webbasedir."movies/covers/".$imdbnr.".jpg")){
			fwrite($htm,'<tr><td style="text-align:center"><img src="covers/'.$imdbnr.'.jpg" alt="cover" /></td></tr>'."\n");
		}
		if(isset($moviearray["media"][0]["description"][0]["cdata"])){
			fwrite($htm,'<tr><td style="font-size: 32px;">'.$moviearray["media"][0]["description"][0]["cdata"].'</td></tr>');
		}
		
		$cast = array();
		foreach($moviearray["media"][0]["cast"][0]["name"] as $member){
			array_push($cast, $member["cdata"]);
		}
		$genres = array();
		foreach($moviearray["media"][0]["genres"][0]["genre"] as $genre){
			array_push($genres, $genre["cdata"]);
			if(!isset($allgenres[$genre["cdata"]])){ $allgenres[$genre["cdata"]] = array(); }
			array_push($allgenres[$genre["cdata"]],$moviearray["media"][0]["title"][0]["cdata"]);
		}
		if(count($cast) > 0){
			fwrite($htm,'<tr><td style="border-top: 1px solid #aaaaaa; font-size: 24px;"><b>Cast:</b><br />'.implode(", ",$cast).'</td></tr>'."\n");
		}
		if(count($genres) > 0){
			fwrite($htm,'<tr><td style="border-top: 1px solid #aaaaaa; font-size: 24px;"><b>Genres:</b> '.implode(", ",$genres).'</td></tr>'."\n");
		}
		fwrite($htm,'
			   </table>
			   </div>
			   </body>
			   </html>
			   ');
		
	}
	
	function xml2array($domnode)
	{
		$nodearray = array();
		$domnode = $domnode->firstChild;
		while (!is_null($domnode))
		{
			$currentnode = $domnode->nodeName;
			$dbg = fopen("/tmp/xmldebug.log","a");
			fwrite($dbg,"\n\n\n");
			fwrite($dbg,$domnode->nodeName);
			fwrite($dbg,"\n");
			fwrite($dbg,$domnode->nodeType);
			fwrite($dbg,"\n");
			fwrite($dbg,$domnode->nodeValue);
			fwrite($dbg,"\n");
			fwrite($dbg,"\n\n\n******** ".XML_ELEMENT_NODE." ******\n\n\n");
			
			
			switch ($domnode->nodeType)
			{
				case XML_TEXT_NODE:
					if(!(trim($domnode->nodeValue) == "")) $nodearray['cdata'] = $domnode->nodeValue;
					break;
				case XML_ELEMENT_NODE:
					if ($domnode->hasAttributes() )
					{
						$elementarray = array();
						$attributes = $domnode->attributes;
						foreach ($attributes as $index => $domobj)
						{
							$elementarray[$domobj->name] = $domobj->value;
						}
					}
					break;
				case 4:
					if(!(trim($domnode->nodeValue) == "")) $nodearray['cdata'] = $domnode->nodeValue;
					break;
			}
			if ( $domnode->hasChildNodes() )
			{
				$nodearray[$currentnode][] = xml2array($domnode);
				if (isset($elementarray))
				{
					$currnodeindex = count($nodearray[$currentnode]) - 1;
					$nodearray[$currentnode][$currnodeindex]['@'] = $elementarray;
				}
			} else {
				if (isset($elementarray) && $domnode->nodeType != XML_TEXT_NODE)
				{
					$nodearray[$currentnode]['@'] = $elementarray;
				}
			}
			$domnode = $domnode->nextSibling;
		}
		return $nodearray;
	}
	
	
	
	function makemainindex(){
		global $webbasedir;
		$htm = fopen($webbasedir."/index.htm","w");
		fwrite($htm,'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
			   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
			   <html xmlns="http://www.w3.org/1999/xhtml">
			   <head>
			   <title>My Movies</title>
			   <style type="text/css">
			   body	{ background: #555555; font-family: sans-serif; padding: 0px; margin: 0px; }
			   ul.btns	{ position: absolute; top: 80px; list-style: none; padding-left: 0px; width: 100%; }
			   ul.btns li	{ padding: 0px; margin: 0px; }
			   ul.btns li a { display: block; font-size: 24px; padding: 5px; border-bottom: 1px solid #ffffff; color: #ffffff; text-decoration: none; }
			   </style>
			   </head>
			   <body>
			   <div style="height: 60px; color: #aaaaaa; font-weight: bold; text-align:center; ; font-size:48px; background:#000000">MY MOVIES</div>
			   <ul class="btns">
			   <li><a href="mov_alphabetical.htm">Alphabetically</a></li>
			   <li><a href="mov_rates.htm">By Rating</a></li>
			   <li><a href="mov_genres.htm">By Genre</a></li>
			   <li><a href="mov_years.htm">By Year</a></li>
			   </ul>
			   </body>
			   </html>');
	}
	
	
	exit;
	?>